package inspur.dao;

import com.sun.net.httpserver.HttpsServer;
import inspur.common.Sha1;
import inspur.entity.CookieTokenObject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class CookieDao {
    /*
        发布一个令牌到浏览器
        该令牌安装到浏览器的cookie中
     */
    public static void addCookieToken(HttpServletResponse response,CookieTokenObject ctObject){
    response.addCookie(ctObject.getUserid());
    response.addCookie(ctObject.getEffectiveTime());
    response.addCookie(ctObject.getAuthorizedIP());
    response.addCookie(ctObject.getSha1());
    }
    /*
        从浏览器中获取cookie信息,创造一个cookieTokenObject令牌对象
        输入：
            HttpServletRequst request
        输出：
            CookieTokenObject ctObject
            null
     */
    public static  CookieTokenObject CreatCookieTokenObject(HttpServletRequest request){
            Cookie userid=getCookieItem(request,"userid");
            if (userid==null) return null;
            Cookie effectiveTime=getCookieItem(request,"effectiveTime");
            if (effectiveTime==null) return null;
            Cookie AuthorizedIP=getCookieItem(request,"AuthorizedIP");
            if (AuthorizedIP==null) return null;
            Cookie sha1=getCookieItem(request,"sha1");
            if (sha1==null) return null;
            CookieTokenObject browserCookieTokenObject=new CookieTokenObject();
            browserCookieTokenObject.setUserid(userid);
            browserCookieTokenObject.setEffectiveTime(effectiveTime);
            browserCookieTokenObject.setAuthorizedIP(AuthorizedIP);
            browserCookieTokenObject.setSha1(sha1);
            return browserCookieTokenObject;

    }
    /*判断令牌的有效性
    输入
        CookieTokenObject cookieTokenObject 令牌对象
    输出
        boolean
            true 该令牌有效
            false 该令牌过期或者失效或者篡改
     */
    public  static  boolean isTrueToken(CookieTokenObject cookieTokenObject,String password,HttpServletRequest request){
        //判断指纹是否被篡改
        String userid=cookieTokenObject.getUserid().getValue();
        String effectiveTime=cookieTokenObject.getEffectiveTime().getValue();
        String AuthorizedIP=cookieTokenObject.getAuthorizedIP().getValue();
        String sha1=cookieTokenObject.getSha1().getValue();
        String cookie_sha1= Sha1.getSha1(userid+effectiveTime+AuthorizedIP+password);
        if (sha1.equals(cookie_sha1)){
            //指纹正确
            long now=new Date().getTime()/1000;
            long cookieTime=Long.parseLong(effectiveTime);
            if (cookieTime>now){
                //时间戳正确
                String request_IP=request.getRemoteAddr();
                if (request_IP.equals(AuthorizedIP)){
                    //授权IP正确
                    //检查完成
                    return true;
                }else{
                    //授权IP错误
                    return false;
                }
            }else{
                //过期
                return  false;

            }

        }else{
            //指纹错误
            return  false;
        }


    }


    /*
    向浏览器中添加一个cookie
    参数：
        HttpServletResponse response
        String name
        String value,int day
     返回：
           void
     */
    public  static void addCookie(HttpServletResponse response,String name,String value,int day){
        int second=60*60*24*day;
        Cookie cookie=new Cookie(name,value);
        cookie.setMaxAge(second);
        response.addCookie(cookie);
    }
    /*
    获取指定cookie的值
    参数 ：
           HttpServletRequest request
           String ItemName  cookie的名字
     返回：
            cookie.getValue()   cookie的值
            nuul                当cookie不存在时
     */
    public static String  getCookieItemValue(HttpServletRequest request,String ItemName){
        // 获得当前路径以及"直接父路径"的所有Cookie对象,如果没有任何Cookie的话,则返回null
        Cookie[] cookies = request.getCookies();
        // 遍历数组,获得具体的Cookie
        if(cookies == null) {

        } else {
            for(int i=0; i<cookies.length; i++) {
                // 获得具体的Cookie
                Cookie cookie = cookies[i];
                // 获得Cookie的名称
                // String name = cookie.getName();
                // String value = cookie.getValue();
                if (cookie.getName().equals(ItemName)){
                    return cookie.getValue();

                }

            }
        }
        return null;

    }
    /*
    从浏览器（request）获取指定的cookie
     参数：
        HttpServletRequest request      request
        String ItemName                 cookie的名字
     返回：
        cookie                          cookie
        null                            当cookie不存在时
     */
    public static Cookie  getCookieItem(HttpServletRequest request,String ItemName){
        // 获得当前路径以及"直接父路径"的所有Cookie对象,如果没有任何Cookie的话,则返回null
        Cookie[] cookies = request.getCookies();
        // 遍历数组,获得具体的Cookie
        if(cookies == null) {
            return  null;
        } else {
            for(int i=0; i<cookies.length; i++) {
                // 获得具体的Cookie
                Cookie cookie = cookies[i];
                // 获得Cookie的名称
                // String name = cookie.getName();
                // String value = cookie.getValue();
                if (cookie.getName().equals(ItemName)){
                    return cookie;

                }

            }
        }
        return null;

    }
        /*
        删除目标计算机上的令牌
         */
        public static  void deleteCookieToken(HttpServletResponse response){
           addCookie(response,"userid","",0);
           addCookie(response,"effectiveTime","",0);
           addCookie(response,"AuthorizedIP","",0);
           addCookie(response,"sha1","",0);
    }

}
